package com.wanmait.movies.util;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.prism.paint.Gradient;

public class ImageSize {
	private  ImageSize() {};
	private static ImageSize imagesize=new ImageSize();
	public static ImageSize getimage() {
		return imagesize;
	}

	public static void ChangeImageSize(String filepath,int width,int height) throws IOException {//������ͼƬ·������Ҫ���õĳ��ȸ߶�
	
	    BufferedImage bi=new BufferedImage(600,600, BufferedImage.TYPE_INT_RGB);//��Ϊһ��������������ͼƬ��С��ͼƬ��С���
	    Graphics gr=bi.getGraphics();//�õ�����
	    ImageIcon img=new ImageIcon(filepath);//��ͼƬת��������
	    gr.drawImage(img.getImage(), 0,0,width, height, null);//����ͼƬ��С
	    File f = new File("d:/mydata/a.jpg");
	    try {
			ImageIO.write(bi,"jpg",f);//���޸����ͼƬ���浽Tomcat
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	public void setImage(String path) {//高宽比1.4，适合电影缩略图，演员导演照片等
		try {
			//System.out.println(path);
            File file = new File(path);
            BufferedInputStream bis = new BufferedInputStream(new  FileInputStream(file));
            BufferedImage bi = ImageIO.read(bis);
            int y = bi.getHeight();//高70
            int x = bi.getWidth();//宽50
            if ((double)y/x > 1.4) {
                bi=bi.getSubimage(0, (int)((y-x*1.4)/2), x, (int)(x*1.4));
            }else {
                bi=bi.getSubimage((int)((double)x-(y/1.4))/2,0,(int)(y/1.4),y);
            }
            int i = file.getPath().lastIndexOf(".");
            String ext = file.getPath().substring(i+1,file.getPath().length());
            ImageIO.write(bi, ext, file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
	public void setPoster(String path) {//海报 宽高比3.0
		try {
            File file = new File(path);
            BufferedInputStream bis = new BufferedInputStream(new  FileInputStream(file));
            BufferedImage bi = ImageIO.read(bis);
            int y = bi.getHeight();//高70
            int x = bi.getWidth();//宽50
            if ((double)x/y < 3) {
                bi=bi.getSubimage(0, (y-(x/3))/2, x, (int)(x/3) );
            }else {
                bi=bi.getSubimage((x-(y*3))/2, 0, y*3, y );
            }
            int i = file.getPath().lastIndexOf(".");
            String ext = file.getPath().substring(i+1,file.getPath().length());
            ImageIO.write(bi, ext, file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
}






 
